#pragma once

#include "../comm/log.hpp"
#include "../comm/util.hpp"

#include <iostream>
#include <string>
#include <cassert>
#include <unordered_map>
#include <fstream>

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        std::string number; // 题目编号，唯一
        std::string title;  // 题目的标题
        std::string star;   // 难度: 简单 中等 困难
        int cpu_limit;      // 题目的时间要求(S)
        int mem_limit;      // 题目的空间要去(KB)
        std::string desc;   // 题目的描述
        std::string header; // 题目预设给用户在线编辑器的代码
        std::string tail;   // 题目的测试用例，需要和header拼接，形成完整代码
    };

    const std::string question_list = "./questions/questions.list"; //题目列表路径
    const std::string questins_path = "./questions/";   //用于拼接题目编号路径
    class Model
    {
    private:
        std::unordered_map<std::string, Question> _questions;
    public:
        Model(){
            //构造对象时，加载题库至内存中
            assert(LoadQuestionList(question_list));
        }
        ~Model(){}
    private:
    //加载题库所有题目到内存中
        bool LoadQuestionList(const std::string &question_list){
            std::fstream ifs(question_list);
            if(!ifs.is_open())
            {
                LOG(FATAL) << "加载题库失败,请检查是否存在题库文件" << "\n";
                return false;
            }
            
            //循环去读题目列表文件
            std::string line;
            while(std::getline(ifs,line))
            {
                std::vector<std::string> tokens;
                StringUtil::Split(line," ",&tokens);
                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败, 请检查文件格式" << "\n";
                    continue;
                }

                //获取题目相关属性
                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = std::stoi(tokens[3]);
                q.mem_limit = std::stoi(tokens[4]);

                //拼接题目编号文件路径
                std::string path = questins_path;
                path += q.number;
                path += "/";
                
                //获取题目详细描述以及相关代码
                FileUtil::ReadFile(path + "desc.txt",&(q.desc),true);
                FileUtil::ReadFile(path + "header.cpp",&(q.header),true);
                FileUtil::ReadFile(path + "tail.cpp",&(q.tail),true);

                //将题目添加进内存
                _questions.insert(std::make_pair(q.number,q));
            }
            
            LOG(INFO) << "题库加载完成,加载题目数量: " << _questions.size() << "\n";
            ifs.close();

            return true;
        }
    public:
        bool GetAllQuestions(std::vector<Question> *out){
            if(_questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }

            for(auto &q : _questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestions(const std::string &number, Question *q){
            auto it = _questions.find(number);
            if(it == _questions.end())
            {
                //题目不存在
                LOG(ERROR) << "编号为 " + number + " 号的题目不存在" << "\n";
                return false;
            }

            *q = it->second;
            return true;
        }
    };
}
